﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="WebDbContextStorage.cs" company="">
//   
// </copyright>
// <summary>
//   The web db context storage.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

#region

using System.Collections.Generic;
using System.Data.Entity;
using System.Web;

#endregion

namespace Common.Data
{
    /// <summary>
    /// The web db context storage.
    /// </summary>
    public class WebDbContextStorage : IDbContextStorage
    {
        /// <summary>
        /// The storag e_ key.
        /// </summary>
        private const string STORAGE_KEY = "HttpContextObjectContextStorageKey";

        /// <summary>
        /// Initializes a new instance of the <see cref="WebDbContextStorage"/> class.
        /// </summary>
        /// <param name="app">
        /// The app.
        /// </param>
        public WebDbContextStorage(HttpApplication app)
        {
            app.EndRequest += (sender, args) =>
            {
                DbContextManager.CloseAllDbContexts();
                HttpContext.Current.Items.Remove(STORAGE_KEY);
            };
        }

        /// <summary>
        /// The get db context for key.
        /// </summary>
        /// <param name="key">
        /// The key.
        /// </param>
        /// <returns>
        /// The <see cref="DbContext"/>.
        /// </returns>
        public DbContext GetDbContextForKey(string key)
        {
            SimpleDbContextStorage storage = GetSimpleDbContextStorage();
            return storage.GetDbContextForKey(key);
        }

        /// <summary>
        /// The set db context for key.
        /// </summary>
        /// <param name="factoryKey">
        /// The factory key.
        /// </param>
        /// <param name="context">
        /// The context.
        /// </param>
        public void SetDbContextForKey(string factoryKey, DbContext context)
        {
            SimpleDbContextStorage storage = GetSimpleDbContextStorage();
            storage.SetDbContextForKey(factoryKey, context);
        }

        /// <summary>
        /// The get all db contexts.
        /// </summary>
        /// <returns>
        /// The <see cref="IEnumerable"/>.
        /// </returns>
        public IEnumerable<DbContext> GetAllDbContexts()
        {
            SimpleDbContextStorage storage = GetSimpleDbContextStorage();
            return storage.GetAllDbContexts();
        }

        /// <summary>
        /// The get simple db context storage.
        /// </summary>
        /// <returns>
        /// The <see cref="SimpleDbContextStorage"/>.
        /// </returns>
        private SimpleDbContextStorage GetSimpleDbContextStorage()
        {
            HttpContext context = HttpContext.Current;
            SimpleDbContextStorage storage = context.Items[STORAGE_KEY] as SimpleDbContextStorage;
            if (storage == null)
            {
                storage = new SimpleDbContextStorage();
                context.Items[STORAGE_KEY] = storage;
            }

            return storage;
        }
    }
}